package com.springboot.security.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @Author: TongRui乀
 * @Date: 2019/1/24 14:46
 * @description：
 */

@Slf4j
@Component
public class TestUserDetailService implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    private final String ROLE = "ROLE_";

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {

        // 测试不查数据库
        String password = userName;

        String encodePassword = passwordEncoder.encode(password);

        // 账户是否启用 激活的概念 如果用户已启用，则设置为 true
        boolean enable = true;
        // 账户是否过期 如果帐户尚未过期，则设置为 true
        boolean accountNonExpired = true;
        // 密码是否过期 如果凭据未过期，则设置为 true
        boolean credentialsNonExpired = true;
        // 账户为被锁定 如果帐户未锁定，则设置为 true
        boolean accountNonLocked = true;
        // 权限集合 如果他们提供了正确的用户名和密码并且启用了用户，则应授予调用者权限。不是空的。
        ArrayList<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
        // 默认添加一个角色 为admin
        grantedAuthorityList.add(new SimpleGrantedAuthority(ROLE + "ADMIN"));
        // 添加一个权限为 queryUser
        grantedAuthorityList.add(new SimpleGrantedAuthority("queryUser"));
        // 简单版本 默认为true  避免这些 概念性的设置
//        new User(username, encodePassword, grantedAuthorityList);　　
        User user = new User(userName, encodePassword, enable, accountNonExpired, credentialsNonExpired, accountNonLocked, grantedAuthorityList);
        log.info("登录认证:", user.toString());

        return user;
    }
}
